package com.sgzn.sgpay.service.impl;

import com.sgzn.sgpay.enums.OrderStateEnum;
import com.sgzn.sgpay.enums.OrderTypeEnum;
import com.sgzn.sgpay.model.request.SgsBizOrderQueryPageRequest;
import com.sgzn.sgpay.model.response.CurrentPageInfo;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.BizOrderQueryPageResult;
import com.sgzn.sgpay.model.response.vo.BizOrderVo;
import com.sgzn.sgpay.persistence.dao.TBizOrderMapper;
import com.sgzn.sgpay.persistence.domain.TBizOrder;
import com.sgzn.sgpay.persistence.domain.TBizOrderExample;
import com.sgzn.sgpay.service.SgsBizOrderService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import com.sgzn.sgpay.util.PageCalculator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务订单管理服务接口实现类
 * @author huangsenguang
 * @version Id: SgsBizOrderServiceImpl.java, v 0.1 2018/4/25 21:02 huangsenguang Exp $$
 */
@Service
@Slf4j
public class SgsBizOrderServiceImpl implements SgsBizOrderService {
    @Autowired
    private TBizOrderMapper tBizOrderMapper;

    @Override
    public SgsResultResponse<BizOrderQueryPageResult> queryPage(SgsBizOrderQueryPageRequest request) {
        TBizOrderExample example = this.getTBizOrderExampleByPage(request);
        //获取总数
        int totalCount = this.tBizOrderMapper.countByExample(example);
        CurrentPageInfo pageInfo = PageCalculator.getPageInfo(totalCount, request.getPageQueryParam().getPageSize(),
                request.getPageQueryParam().getCurrentPage());
        StringBuilder clause = new StringBuilder();
        //加上起始和结束行号进行列表查询
        clause.append("created_at desc ");
        clause.append("limit ").append(pageInfo.getStartIndex()).
                append(",").append(pageInfo.getPageSize());
        example.setOrderByClause(clause.toString());
        List<TBizOrder> list = tBizOrderMapper.selectByExample(example);

        return SgsResponseUtil.INSTANCE.success(assembleResponse(totalCount, pageInfo, list));
    }

    /**
     * 组装响应
     * @param totalCount
     * @param pageInfo
     * @param list
     * @return
     */
    private BizOrderQueryPageResult assembleResponse(int totalCount, CurrentPageInfo pageInfo, List<TBizOrder> list) {
        BizOrderQueryPageResult result = new BizOrderQueryPageResult();
        result.setBizOrderList(getBizOrderVos(list));
        result.setCurrentPage(pageInfo.getCurrentPage());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalCount(totalCount);
        result.setTotalPage(pageInfo.getTotalPage());
        return result;
    }

    /**
     * 对象转换
     * @param list
     * @return
     */
    private List<BizOrderVo> getBizOrderVos(List<TBizOrder> list) {
        List<BizOrderVo> volist = new ArrayList<>(list.size());
        for (TBizOrder tBizOrder : list) {
            BizOrderVo vo = new BizOrderVo();
            vo.setOrderId(tBizOrder.getOrderId());
            vo.setBizOrderId(tBizOrder.getBizOrderId());
            vo.setBizType(tBizOrder.getBizType());
            vo.setBizTypeName(OrderTypeEnum.getDescByCode(tBizOrder.getBizType()));
            vo.setBizState(tBizOrder.getBizState());
            vo.setBizStateName(OrderStateEnum.explain(tBizOrder.getBizState()));
            vo.setBizAmount(tBizOrder.getBizAmount());
            vo.setOrderDevice(tBizOrder.getOrderDevice());
            vo.setOrderShop(tBizOrder.getOrderShop());
            vo.setOrderMerchant(tBizOrder.getOrderMerchant());
            vo.setUserId(tBizOrder.getUserId());
            vo.setCreatedAt(tBizOrder.getCreatedAt());
            vo.setUpdatedAt(tBizOrder.getUpdatedAt());
            vo.setCreatedBy(tBizOrder.getCreatedBy());
            vo.setUpdatedBy(tBizOrder.getUpdatedBy());
            volist.add(vo);
        }
        return volist;
    }

    /**
     * 获取查询对象
     * @param request
     * @return
     */
    private TBizOrderExample getTBizOrderExampleByPage(SgsBizOrderQueryPageRequest request){
        TBizOrderExample countExample = new TBizOrderExample();
        TBizOrderExample.Criteria cra = countExample.createCriteria();
        if(StringUtils.isNotBlank(request.getOrderId())){
            cra.andOrderIdEqualTo(request.getOrderId());
        }
        if(StringUtils.isNotBlank(request.getUserId())){
            cra.andUserIdEqualTo(request.getUserId());
        }
        if(StringUtils.isNotBlank(request.getMchId())){
            cra.andOrderMerchantEqualTo(request.getMchId());
        }
        if(StringUtils.isNotBlank(request.getBizState())){
            cra.andBizStateEqualTo(request.getBizState());
        }
        if(StringUtils.isNotBlank(request.getBizType())){
            cra.andBizTypeEqualTo(request.getBizType());
        }
        return countExample;
    }
}

